package com.prothink.threadproject.controller;

import com.prothink.threadproject.common.R;
import com.prothink.threadproject.common.Result;
import com.prothink.threadproject.entity.FormLogin;
import com.prothink.threadproject.entity.User;
import com.prothink.threadproject.exception.BisnessException;
import com.prothink.threadproject.service.UserService;
import com.prothink.threadproject.vo.AccountForm;
import com.prothink.threadproject.vo.ForgetPwdVo;
import com.prothink.threadproject.vo.UserInfoVo;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public Result<String> register(@Valid @RequestBody AccountForm accountForm){
        userService.registUser(accountForm);
        return Result.success("注册成功");
    }
    @PostMapping("/login")
    public Result<UserInfoVo> login(@Valid @RequestBody AccountForm accountForm){
        UserInfoVo userInfoVo = userService.login(accountForm);
        return Result.success(userInfoVo);
    }

    /**
     * 重置密码
     * 1.查询用户的邮箱
     * 2.生成一个随机的密码: 数字+字母的组合,长度为6
     * 3.邮件发送
     * @param forgetPwdVo
     * @return
     */
    @PostMapping("/resetPwd")
    public Result<String> resetPwd(@Valid @RequestBody ForgetPwdVo forgetPwdVo){
        userService.resetPwd(forgetPwdVo);
        return Result.success();
    }

    @PostMapping("/validUserAccount")
    public Result<String> validUserAccount(@Valid @RequestBody ForgetPwdVo forgetPwdVo){
        if (StringUtils.isEmpty(forgetPwdVo.getAccount())){
            return Result.failure("账号不能为空",-1);
        }
        FormLogin user = userService.getUserByName(forgetPwdVo.getAccount());
        if (user == null){
            return Result.failure("该账号不存在: " + forgetPwdVo.getAccount(), -1);
        }
        return Result.success();
    }

    @PostMapping("/validIdCard")
    public Result<String> validIdCard(@Valid @RequestBody ForgetPwdVo forgetPwdVo){
        if (StringUtils.isEmpty(forgetPwdVo.getAccount())){
            return Result.failure("账号不能为空");
        }
        if (StringUtils.isEmpty(forgetPwdVo.getIdCard())){
            return Result.failure("身份证号不能为空");
        }
        FormLogin user = userService.getUserByName(forgetPwdVo.getAccount());
        if (user == null){
            return Result.failure("该账号不存在: " + forgetPwdVo.getAccount());
        }
        if (!user.getIdCard().equals(forgetPwdVo.getIdCard())){
            return Result.failure("身份证号验证失败: " + forgetPwdVo.getAccount());
        }

        return Result.success();
    }
}
